#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    ll a0, a1, b0, b1;
    cin >> a0 >> a1 >> b0 >> b1;
    if ((b1 % b0) || (a0 % a1)) {
      cout << 0 << endl;
      continue;
    }
    ll k = b1 / b0;
    ll ans = 0;
    for (ll i = 1; i * i <= b0; i++) {
      if (b0 % i) continue;
      ll j = b0 / i;
      if (gcd(j, k) == 1) {
        ll x = k * i;
        if (gcd(a0, x) == a1) ans++;
      }
      if (j != i && gcd(i, k) == 1) {
        ll x = k * j;
        if (gcd(a0, x) == a1) ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}